package com.fooddelivery;

import javax.swing.*;
import java.awt.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class OrderManagementFrame extends JFrame {
    private JTextArea orderInfoArea;

    public OrderManagementFrame(int userId) {
        setTitle("Order Management");
        setSize(500, 400);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());

        orderInfoArea = new JTextArea();
        orderInfoArea.setEditable(false);
        panel.add(new JScrollPane(orderInfoArea), BorderLayout.CENTER);

        loadOrders(userId);

        add(panel);
    }

    private void loadOrders(int userId) {
        try (Connection conn = DatabaseConnection.getConnection()) {
            String sql = "SELECT * FROM orders WHERE user_id = ?";
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setInt(1, userId);
            ResultSet rs = stmt.executeQuery();

            StringBuilder sb = new StringBuilder();
            while (rs.next()) {
                sb.append("Order ID: ").append(rs.getInt("id")).append("\n");
                sb.append("Restaurant ID: ").append(rs.getInt("restaurant_id")).append("\n");
                sb.append("Total Price: ").append(rs.getDouble("total_price")).append("\n");
                sb.append("Order Date: ").append(rs.getTimestamp("order_date")).append("\n\n");
            }
            orderInfoArea.setText(sb.toString());
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
}
